package org.example.game.entity;

import org.example.game.GamePanel;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.List;

public class Tank extends GameObject {
    public static int TANK_SPEED = 5;  // 现在是可配置的
    private static final int TANK_SIZE = 40;
    
    private int direction; // 0:上, 1:右, 2:下, 3:左
    private TankType tankType;
    private boolean[] moving = new boolean[4]; // 上右下左的移动状态
    private Color color;
    private long lastFireTime = 0;
    private static final long FIRE_COOLDOWN = 500; // 发射冷却时间（毫秒）
    private int health = 100;  // 添加生命值
    private boolean isDestroyed = false;  // 是否被摧毁
    private long destroyedTime = 0;  // 记录被摧毁的时间
    private static final long RESPAWN_TIME = 3000;  // 重生时间（毫秒）

    public enum TankType {
        PLAYER,
        PLAYER2,
        AI
    }

    public Tank(int x, int y, TankType type) {
        super(x, y, TANK_SIZE, TANK_SIZE);
        this.tankType = type;
        this.direction = 0;
        
        switch (type) {
            case PLAYER:
                color = Color.GREEN;
                break;
            case PLAYER2:
                color = Color.BLUE;
                break;
            case AI:
                color = Color.RED;
                break;
        }
    }

    @Override
    public void draw(Graphics g) {
        if (isDestroyed) {
            drawExplosion(g);
            return;
        }
        
        g.setColor(color);
        g.fillRect(x, y, width, height);
        
        // 画炮管
        Graphics2D g2d = (Graphics2D) g;
        g2d.setStroke(new BasicStroke(4));
        int centerX = x + width / 2;
        int centerY = y + height / 2;
        int gunLength = 20;
        
        switch (direction) {
            case 0: g2d.drawLine(centerX, centerY, centerX, centerY - gunLength); break;
            case 1: g2d.drawLine(centerX, centerY, centerX + gunLength, centerY); break;
            case 2: g2d.drawLine(centerX, centerY, centerX, centerY + gunLength); break;
            case 3: g2d.drawLine(centerX, centerY, centerX - gunLength, centerY); break;
        }

        // 绘制生命值条
        g.setColor(Color.RED);
        g.fillRect(x, y - 10, width, 5);
        g.setColor(Color.GREEN);
        g.fillRect(x, y - 10, width * health / 100, 5);
    }

    private void drawExplosion(Graphics g) {
        int centerX = x + width / 2;
        int centerY = y + height / 2;
        
        // 绘制爆炸效果
        g.setColor(Color.ORANGE);
        for (int i = 0; i < 8; i++) {
            double angle = i * Math.PI / 4;
            int x1 = (int) (centerX + Math.cos(angle) * width/2);
            int y1 = (int) (centerY + Math.sin(angle) * height/2);
            int x2 = (int) (centerX + Math.cos(angle) * width);
            int y2 = (int) (centerY + Math.sin(angle) * height);
            g.drawLine(x1, y1, x2, y2);
        }
        
        g.setColor(Color.RED);
        g.fillOval(centerX - width/4, centerY - height/4, width/2, height/2);
    }

    public void update() {
        int newX = x;
        int newY = y;
        
        if (moving[0]) newY -= TANK_SPEED;
        if (moving[1]) newX += TANK_SPEED;
        if (moving[2]) newY += TANK_SPEED;
        if (moving[3]) newX -= TANK_SPEED;
        
        // 临时保存原位置
        int oldX = x;
        int oldY = y;
        
        // 尝试移动
        x = newX;
        y = newY;
        
        // 如果发生碰撞，恢复原位置
        if (checkCollision()) {
            x = oldX;
            y = oldY;
        }
    }

    private boolean checkCollision() {
        // 获取当前游戏面板中的墙壁列表
        List<Wall> walls = GamePanel.getWalls();
        
        // 检查与所有墙壁的碰撞
        for (Wall wall : walls) {
            if (this.intersects(wall)) {
                return true;
            }
        }
        
        // 检查与其他坦克的碰撞
        List<Tank> tanks = GamePanel.getTanks();
        for (Tank otherTank : tanks) {
            if (otherTank != this && this.intersects(otherTank)) {
                return true;
            }
        }
        
        return false;
    }

    public void handleKeyPress(int keyCode) {
        if (tankType == TankType.PLAYER) {
            switch (keyCode) {
                case KeyEvent.VK_W: 
                    moving[0] = true; 
                    direction = 0; 
                    fire();  // 移动时发射子弹
                    break;
                case KeyEvent.VK_D: 
                    moving[1] = true; 
                    direction = 1; 
                    fire();
                    break;
                case KeyEvent.VK_S: 
                    moving[2] = true; 
                    direction = 2; 
                    fire();
                    break;
                case KeyEvent.VK_A: 
                    moving[3] = true; 
                    direction = 3; 
                    fire();
                    break;
            }
        } else if (tankType == TankType.PLAYER2) {
            switch (keyCode) {
                case KeyEvent.VK_UP: 
                    moving[0] = true; 
                    direction = 0;
                    fire();
                    break;
                case KeyEvent.VK_RIGHT: 
                    moving[1] = true; 
                    direction = 1;
                    fire();
                    break;
                case KeyEvent.VK_DOWN: 
                    moving[2] = true; 
                    direction = 2;
                    fire();
                    break;
                case KeyEvent.VK_LEFT: 
                    moving[3] = true; 
                    direction = 3;
                    fire();
                    break;
            }
        }
    }

    public void handleKeyRelease(int keyCode) {
        if (tankType == TankType.PLAYER) {
            switch (keyCode) {
                case KeyEvent.VK_W: moving[0] = false; break;
                case KeyEvent.VK_D: moving[1] = false; break;
                case KeyEvent.VK_S: moving[2] = false; break;
                case KeyEvent.VK_A: moving[3] = false; break;
            }
        } else if (tankType == TankType.PLAYER2) {
            switch (keyCode) {
                case KeyEvent.VK_UP: moving[0] = false; break;
                case KeyEvent.VK_RIGHT: moving[1] = false; break;
                case KeyEvent.VK_DOWN: moving[2] = false; break;
                case KeyEvent.VK_LEFT: moving[3] = false; break;
            }
        }
    }

    public TankType getTankType() {
        return tankType;
    }

    public int getDirection() {
        return direction;
    }

    public Bullet fire() {
        long currentTime = System.currentTimeMillis();
        if (currentTime - lastFireTime < FIRE_COOLDOWN) {
            return null;
        }
        
        lastFireTime = currentTime;
        int bulletX = x + width / 2;
        int bulletY = y + height / 2;
        
        // 根据方向调整子弹的初始位置
        switch (direction) {
            case 0: bulletY -= height / 2; break; // 上
            case 1: bulletX += width / 2; break;  // 右
            case 2: bulletY += height / 2; break; // 下
            case 3: bulletX -= width / 2; break;  // 左
        }
        
        return new Bullet(bulletX, bulletY, direction, this);
    }

    public void damage(int amount) {
        health -= amount;
        if (health <= 0 && !isDestroyed) {
            isDestroyed = true;
            destroyedTime = System.currentTimeMillis();
        }
    }

    public boolean isDestroyed() {
        return isDestroyed;
    }

    public void respawn() {
        isDestroyed = false;
        health = 100;
        // 根据坦克类型设置重生位置
        switch (tankType) {
            case PLAYER:
                x = 100;
                y = 100;
                break;
            case PLAYER2:
                x = 700;
                y = 500;
                break;
            case AI:
                x = 700;
                y = 500;
                break;
        }
    }

    public long getDestroyedTime() {
        return destroyedTime;
    }

    public static long getRespawnTime() {
        return RESPAWN_TIME;
    }
} 